package com.nativetalk.service.four.note;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.TdNtNote;
import com.nativetalk.bean.note.TdNtNoteList;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.dao.TdNtNoteDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2016/11/1.
 */
@Service
public class FourNoteServiceImpl implements FourNoteService{
    private static Logger logger = Logger.getLogger(FourNoteServiceImpl.class);
    @Resource
    private TdNtNoteDao tdNtNoteDao;
    @Override
    public RetInfo insert(TdNtNote tdNtNote) {
        String logInfo = this.getClass().getName() + ":insert:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tdNtNoteDao.insertNote(tdNtNote);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("成功");
            retInfo.setObj(tdNtNote.getNote_id());
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo update(TdNtNote tdNtNote) {
        String logInfo = this.getClass().getName() + ":update:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tdNtNoteDao.updateNote(tdNtNote);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("成功");

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo delete(TdNtNote tdNtNote) {
        String logInfo = this.getClass().getName() + ":delete:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tdNtNoteDao.updateState(tdNtNote);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("成功");

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo list(Page page) {
        String logInfo = this.getClass().getName() + ":coursesearch:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();

            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtNoteDao.findNoteByMembIdSize(page);//总条数

                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);
            List<TdNtNoteList> tdNtNoteLists = tdNtNoteDao.findNoteByMembIdPage(page);

            result.put("totalPage", totalPage);
            result.put("notes", tdNtNoteLists);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取列表成功");
            retInfo.setObj(result);



        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
